package live.ifox.helper;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

public class CustomProfile {
    private SharedPreferences sp;

    public CustomProfile(Context context) {
        sp = context.getSharedPreferences("app_profile", Context.MODE_PRIVATE);
    }

    public CustomProfile(Context context, String fileName) {
        sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    public boolean contains(String key) {
        return sp.contains(key);
    }

    public void save(Map<String, String> map) {
        SharedPreferences.Editor editor = sp.edit();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            editor.putString(entry.getKey(), entry.getValue());
        }
        // editor.commit(); 同步提交，有返回
        editor.apply(); // 异步提交，无返回
    }

    public void remove(String key) {
        if (sp.contains(key))
            sp.edit().remove(key).apply();
    }

    public void clear() {
        sp.edit().clear().apply();
    }

    public Map<String, ?> load() {
        return sp.getAll();
    }

    public void putString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public void putInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    public String getString(String key) {
        return sp.getString(key, "");
    }

    public int getInt(String key) {
        return sp.getInt(key, 0);
    }
}
